package com.sgb.kjwl.viewmodel.home;

import android.text.TextUtils;
import android.util.Log;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.lease.AskLeaseSheBeiEntity;
import com.sgb.kjwl.model.entity.request.LeaseAskFilterJson;
import com.sgb.kjwl.model.entity.supplier3.DictEntity3;
import com.sgb.kjwl.repository.HomeRepertory;
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity;
import com.sgb.kjwl.view.ui.fragment.home.ChildLeaseAskFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import java.util.Arrays;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

@Deprecated
public class ChildLeaseAskFragmentModel extends BaseViewModel {

    private ChildLeaseAskFragment askLeaseFragment;
    private HomeRepertory repertory;
//    public ObservableField<MatchEntity> matchEntity = new ObservableField<>(new MatchEntity());

    public ChildLeaseAskFragmentModel(ChildLeaseAskFragment askLeaseF, HomeRepertory repertory) {
        this.askLeaseFragment = askLeaseF;
        this.repertory = repertory;
    }

    //获取一级类型
    public void getFilterTypeModel(String type) {
        APIManager.getInstance().getLeaseMapAPI().getFistcategory(type).enqueue(new Callback<BaseEntity<List<DictEntity3>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<DictEntity3>>> call, Response<BaseEntity<List<DictEntity3>>> response) {
                if (response.body() == null || response.body().getState() == null) return;
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    askLeaseFragment.showCategoryList(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<DictEntity3>>> call, Throwable t) {
            }
        });

    }

    public void getDataList(String categoryType, String classNo, String typeNo, String cityId, String addrNo, String sortNo, int page) {

        if (!TextUtils.isEmpty(categoryType)) {
            //因为数据下标是从0开始的，儿接口数据是从1开始的，需要加1
            categoryType = Integer.parseInt(categoryType) + 1 + "";
        }
        LeaseAskFilterJson filterJson = new LeaseAskFilterJson();
        filterJson.setProvinceNo(repertory.getAreaCode());
        filterJson.setCityNo(cityId);
        filterJson.setCurrent(page);
        filterJson.setSortWay(sortNo);
        filterJson.setType(categoryType);
        filterJson.setFirstCategoryNo(classNo);
        filterJson.setSecondCategoryNo(typeNo);
        if (!TextUtils.isEmpty(addrNo)) {
            List<String> addrNos = Arrays.asList(addrNo.split(","));
            filterJson.setAreaNos(addrNos);
        }
        getAskRentLeaseAll(filterJson);

//        Map map = new HashMap();
//        map.put("areacode", repertory.getAreaCode());
//        map.put("rentalTitle", "");
//        map.put("PageNum", page);
//        map.put("pageSize", 10);
//        map.put("categoryType", categoryType);//租赁类目
//        map.put("categoryParentId", classNo);//分类ID
//        map.put("categoryChildId", typeNo);//类型ID
//        map.put("rentAddressProvinceId", repertory.getAreaCode());//区域编号
//        map.put("cityId", cityId);//市ID
//        map.put("sortWay", sortNo);//排序
//        if (!TextUtils.isEmpty(addrNo)) {
//            map.put("areaId", addrNo);//区,多选
//        }
//
//        APIManager.getInstance().getMachineAPI().getEquList(map).enqueue(new Callback<BaseEntity<ListResponse<List<AskLeaseSheBeiEntity>>>>() {
//            @Override
//            public void onResponse(Call<BaseEntity<ListResponse<List<AskLeaseSheBeiEntity>>>> call, Response<BaseEntity<ListResponse<List<AskLeaseSheBeiEntity>>>> response) {
//                if (response.body() == null || response.body().getState() == null) {
//                    return;
//                }
//
//                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
//                    if (null != response.body().getData()) {
//                        askLeaseFragment.showList(response.body().getData().getList());
//                    } else {
//                        MToast.showToast(askLeaseFragment.getActivity(), "暂无数据");
//                    }
//                }
//            }
//
//            @Override
//            public void onFailure(Call<BaseEntity<ListResponse<List<AskLeaseSheBeiEntity>>>> call, Throwable t) {
//                Log.v("dd", "sdsdd");
//            }
//        });
    }

    public void getAskRentLeaseAll(LeaseAskFilterJson leaseAskFilterJson) {
        APIManager.getInstance().getMachineAPI().getAskRentLeaseAll(leaseAskFilterJson).enqueue(new Callback<BaseEntity<ListResponse<List<AskLeaseSheBeiEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<AskLeaseSheBeiEntity>>>> call, Response<BaseEntity<ListResponse<List<AskLeaseSheBeiEntity>>>> response) {
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (null != response.body().getData()) {
                        askLeaseFragment.showList(response.body().getData().getList());
                    } else {
                        MToast.showToast(askLeaseFragment.getActivity(), "暂无数据");
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<AskLeaseSheBeiEntity>>>> call, Throwable t) {
                Log.v("dd", "sdsdd");
            }
        });
    }

    public void getH5Url(String categoryType, String id) {
        String urlName = "";
        switch (categoryType) {
            case "1"://机械
                urlName = "mechanicsRentDetails";
                break;
            case "2"://设备
                urlName = "equipmentRentDetails";
                break;
            case "3"://车辆
                urlName = "vehicleRentDetails";
                break;
            case "4"://房产
                urlName = "houseRentDetails";
                break;
            case "5"://土地
                urlName = "landRentDetails";
                break;
            case "6"://工厂
                urlName = "factoryRentDetails";
                break;
            case "7"://农机
                urlName = "farmRentDetails";
                break;
        }
        if (TextUtils.isEmpty(urlName)) {
            return;
        }

        DialogHelper.showProgressDialog(askLeaseFragment.getActivity(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().getUrlSetting(urlName).enqueue(new Callback<BaseEntity<H5ReEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<H5ReEntity>> call, Response<BaseEntity<H5ReEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    String url = response.body().getData().getUrl() + "?id=" + id;
//                    WebViewX5HostActivity.start(askLeaseFragment.getActivity(), url)
                    WebViewX5HostActivity.start(askLeaseFragment.getActivity(), url,id,"asklease",categoryType);;
                } else {
                    MToast.showToast(askLeaseFragment.getContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<H5ReEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }


}
